const Client = require('ftp');

//@Bean
class ZcFtpServiceConfig {

	//@Value(ZcFtpService.host)
	host;
	//@Value(ZcFtpService.port)
 	port;
 	//@Value(ZcFtpService.user)
 	user;
 	//@Value(ZcFtpService.password)
 	password;

 	//connect state
 	isConnect = false;

 	/**
		()=>Promise<Client>
		login in to the FTP service of the target host using the configuration information.
 	*/
 	doConnect(){

 		this.client = new Client();

 		this.client.connect(this)

 		return new Promise( (resolve,reject) => {
	 		this.client.on('ready', async () => {
				try{
					this.isConnect = true;
					resolve(this.client);
				}catch(e){
					resolve(e)
				}
			});
 		})

 	}

	/**
		close the connect.
		()=>void !e
		the follow situation will throw execption
		e1:NOT_CONNECT not connected.
		e2:CONNECT_ERROR can not close the connection.
	*/
 	async close(){
 		if(!this.isConnect){
 			throw `NO_CONNECT`
 		}
 		this.client.end();
 		this.isConnect = false;
 	}


}

module.exports = {ZcFtpServiceConfig}